import { nextTick } from "../utils"

let queue = []
let has = {}  //做列表  维护存放了哪些watcher
let pending = false

function flushSchedulerQueue () {
  for (let i = 0; i < queue.length; i++) {
    queue[i].run()
  }
  queue = []
  has = {}
  pending = false
}

// 等待同步执行完 才执行异步操作  (批处理 )
export function queueWatcher (watcher) {
  const id = watcher.id
  if (has[id] == null) {
    queue.push(watcher)
    has[id] = true
    // 开启一次更新  多次操作 触发一次(防抖)
    if (!pending) {
      nextTick(flushSchedulerQueue, 0)
    }

  }
}